package com.atzxh.guigu.juc.queue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author 小红同学
 * @create 2023-04-28-16:17
 * @description     阻塞队列相关方法演示
 */
public class BlockingQueueDemo {
    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<String>(3);
        // 第一组
//        System.out.println(blockingQueue.add("1"));
//        System.out.println(blockingQueue.add("1"));
//        System.out.println(blockingQueue.add("1"));
        // System.out.println(blockingQueue.add("1")); // Exception in thread "main" java.lang.IllegalStateException: Queue full

//        System.out.println(blockingQueue.remove());
//        System.out.println(blockingQueue.remove());
//        System.out.println(blockingQueue.remove());
        // System.out.println(blockingQueue.remove()); // Exception in thread "main" java.util.NoSuchElementException

        // 第二组
//        System.out.println(blockingQueue.offer("a"));
//        System.out.println(blockingQueue.offer("b"));
//        System.out.println(blockingQueue.offer("c"));
//        System.out.println(blockingQueue.offer("www")); // false
//
//        System.out.println(blockingQueue.poll());
//        System.out.println(blockingQueue.poll());
//        System.out.println(blockingQueue.poll());
//        System.out.println(blockingQueue.poll()); // null

        // 第三组
//        blockingQueue.put("1");
//        blockingQueue.put("1");
//        blockingQueue.put("1");
        //blockingQueue.put("1"); // 此时会一直处于阻塞状态

//        System.out.println(blockingQueue.take());
//        System.out.println(blockingQueue.take());
//        System.out.println(blockingQueue.take());
        //System.out.println(blockingQueue.take()); // 此时会一直处于阻塞状态

        // 第四组
        System.out.println(blockingQueue.offer("1"));
        System.out.println(blockingQueue.offer("1"));
        System.out.println(blockingQueue.offer("1"));
        System.out.println(blockingQueue.offer("1",3L, TimeUnit.SECONDS));
    }
}
